//
//  UUHybridOpenSpecDetailHandler.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 27/02/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUHybridOpenSpecDetailHandler.h"
#import "UUProductUtility.h"

@implementation UUHybridOpenSpecDetailHandler

- (NSString *)name {
    return @"openSpecDetail";
}

- (void)executeWithParams:(id)params callbackWith:(void (^)(id))callback {
    UUProductSpecEditVC *specEditVC = [self createSpecVCWithParams:params];
    WEAK_SELF();
    [specEditVC setDidUpdateSpecBlock:^(UUProductSpecModel *spec) {
        if (spec != nil) {
            callback([UUProductUtility convertProductSpecModelToFormDict:spec]);
        } else {
            callback(@{});
        }
        
        [weakSelf.context.hostVC.navigationController popViewControllerAnimated:YES];
    }];
    
    [self.context.hostVC.navigationController pushViewController:specEditVC animated:YES];
}

- (UUProductSpecEditVC *)createSpecVCWithParams:(id)params {
    UUProductSpecEditVC *specEditVC = [UUProductSpecEditVC instanceOfAppropriate];
    
    NSDictionary *specDict = params[@"spec"];
    if (specDict) {
        specEditVC.spec = [[UUProductSpecModel alloc] initWithData:specDict];
    }
    
    return specEditVC;
}

@end
